package com.cjg.xiancheng;

public class threadPeopleMirror {
    public static void main(String[] args) {
        user me = new user(0, "me");    user you = new user(1, "you");
        new Thread(me).start(); new Thread(you).start();
    }
}

class People{}
class Mirror{}
class user implements Runnable{
    private  static People people=new People();
    private static  Mirror mirror=new Mirror();
    private  int age;

    private String name;

    public user(int age ,String name){
        this.age=age;
        this.name=name;
    }
    @Override
    public void run() {
        try {
            use();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void use() throws InterruptedException {
        if (age==0){

            //这种状态 🔒了两个  people没执行好不能执行mirror 另一个相反 ,一个无法获得mirror 一个无法获得peopel
            synchronized (people){
                System.out.println("people");
//                Thread.sleep(1000);
                synchronized (mirror){
                    System.out.println("mirror");
                }
            }

        }else {
            synchronized (mirror){
                    System.out.println("mirror");
//                Thread.sleep(1000);

            }//这样 保证一个不锁死就好
            synchronized (people){
                System.out.println("people");
            }
        }
    }
}
